package com.gzist.elderlyserviceplatform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzist.elderlyserviceplatform.common.Result;
import com.gzist.elderlyserviceplatform.entity.dto.InfoNewsUpdateDTO;
import com.gzist.elderlyserviceplatform.entity.dto.InfoNewsPageDTO;
import com.gzist.elderlyserviceplatform.entity.po.InfoNews;
import com.gzist.elderlyserviceplatform.entity.vo.InfoNewsVo;
import com.gzist.elderlyserviceplatform.service.InfoNewsService;
import com.gzist.elderlyserviceplatform.service.RedisService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 资讯信息表
 */
@RestController
@Tag(name = "资讯信息管理")
@RequestMapping("/api/admin/infoNews")
@RequiredArgsConstructor
public class InfoNewsControllerA {

    private final InfoNewsService infoNewsService;
    private final RedisService redisService;

    @PostMapping("/add")
    @Operation(summary = "咨询信息添加", description = "InfoNews")
    public Result<Object> add(
            @RequestBody
            @Parameter(description = "资讯信息") InfoNews infoNews
    ) {
        infoNewsService.save(infoNews);
        return Result.success("添加成功");
    }

    @PostMapping("/list")
    @Operation(summary = "资讯查询列表获取")
    public Result<Page<InfoNewsVo>> list(
            @RequestBody
            @Parameter(description = "查询参数") InfoNewsPageDTO infoNewsPageDTO
    ) {
        return Result.success(infoNewsService.queryPage(infoNewsPageDTO), "查询成功");
    }

    @PostMapping("/update")
    @Operation(summary = "根据id修改", description = "infoNewsDto包含id")
    public Result update(
            @RequestBody
            @Parameter(description = "资讯信息") InfoNewsUpdateDTO infoNewsUpdateDTO
    ) {
        infoNewsService.update(infoNewsUpdateDTO);
        return Result.success("修改成功");
    }

    @GetMapping("/detail/{id}")
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "资讯信息查看详情", description = "根据id查询")
    public Result<InfoNews> getUserById(
            @PathVariable(value = "id")
            @Parameter(description = "资讯信息id") String id
    ) {
        redisService.incrementViewCount(id);
        return Result.success(infoNewsService.getById(id), "资讯信息查询成功");
    }

    @PostMapping("/delete/{id}")
    @Operation(summary = "根据id删除", description = "传递资讯信息id,根据id删除信息")
    public Result delete(
            @PathVariable("id")
            @Parameter(description = "资讯id") String id
    ) {
        infoNewsService.removeById(id);
        return Result.success("删除成功");
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "资讯信息批量删除", description = "传递资讯信息id集,根据id批量删除")
    public Result batchDelete(
            @RequestBody
            @Parameter(description = "资讯信息id集") String[] ids
    ) {
        infoNewsService.removeByIds(Arrays.asList(ids));
        return Result.success("删除成功");
    }
}